package com.hxtec.polaris.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hxtec.polaris.form.PayForm;
import com.hxtec.polaris.mapper.ShopOrderMapper;
import com.hxtec.polaris.pay.wx.WXPay;
import com.hxtec.polaris.pay.wx.WXPayRealConfig;
import com.hxtec.polaris.service.PaymentService;
import com.hxtec.polaris.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author Marcus lv
 * @create 2019/12/10 9:34 下午
 */
@Service
@Slf4j
public class PaymentServiceImpl implements PaymentService {
    private final WXPayRealConfig config;
    private final RestTemplate template;
    private final ObjectMapper objectMapper;
    private final ShopOrderMapper shopOrderMapper;
    private final UserService userService;

    public PaymentServiceImpl(WXPayRealConfig config, RestTemplate template, ObjectMapper objectMapper, ShopOrderMapper shopOrderMapper, UserService userService) {
        this.config = config;
        this.template = template;
        this.objectMapper = objectMapper;
        this.shopOrderMapper = shopOrderMapper;
        this.userService = userService;
    }

    @Override
    public String openid(String code) {
        String url = "https://api.weixin.qq.com/sns/jscode2session?" +
                "appid=" + config.getAppID() + "&secret=" + config.getSecret() + "&js_code=" + code + "&grant_type=authorization_code";
        ResponseEntity forEntity = template.getForEntity(url, String.class);
        try {
            return String.valueOf(objectMapper.readTree(String.valueOf(forEntity.getBody())).get("openid"));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Object process(PayForm form) throws Exception {
        if ("miniWx".equals(form.getType())) {
            return miniWx(form);
        }
        throw new Exception("unsupport payment type:" + form.getType());
    }

    private Object miniWx(PayForm form) throws Exception {
        WXPay pay = new WXPay(config, "??????");
        Map<String,Object> orderMap = shopOrderMapper.getPriceByOrderId(form.getOrderId(),userService.getUser().getMobile());
        BigDecimal price = (BigDecimal) orderMap.get("pay_price");
        if (null!=price) {
            Map<String, String> pam = new HashMap<>();
            pam.put("body", String.valueOf(orderMap.get("receiver_name")));
            pam.put("out_trade_no", String.valueOf(form.getOrderId()));
            pam.put("total_fee", String.valueOf(price.multiply(new BigDecimal("1000")).intValue()));
            pam.put("spbill_create_ip", form.getIp());
            pam.put("trade_type", "JSAPI");
            pam.put("openid", form.getCode());
            return pay.unifiedOrder(pam);
        }
        return null;
    }
}
